#include "ScreenView.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QScreen>

ScreenView::ScreenView(QObject* parent) :
	QObject(parent),
	_x0(0), _y0(0),
	_width(0), _height(0),
	_screen(NULL),
    _twin(true),
	_screenNumber(1),
	_showImage(false)
{
	setMode(ScreenMode::Middle_16_9);
	setPos(_x0,_y0);
}

ScreenView::ScreenView(int x, int y, int width, int height, QObject* parent) :
	QObject(parent),
	_x0(x), _y0(y),
	_width(width), _height(height),
	_screen(NULL),
    _twin(true),
	_screenNumber(1),
	_showImage(false)
{
	setMode(ScreenMode::Middle_16_9);
	setPos(x,y);
}

void ScreenView::setGeometry(int width, int height)
{
	_width = width;//-_x0*2;
	_height = height;//-_y0*2;
	updateView();
}

void ScreenView::setMode(ScreenMode::Enum mode)
{
	switch(mode){
	case ScreenMode::Middle_16_9:
		_imageFile = ":/images/monitor.png";
		_ratio = 1331.0/1160;
		_frameSize = QRectF(0.13,0.08,0.13,0.08);
		_xRatioPart = 16;
		_yRatioPart = 9;
		break;
	case ScreenMode::Middle_4_3:
		_imageFile = ":/images/monitor2.png";
		_ratio = 1050.0/1160;
		_frameSize = QRectF(0.13,0.08,0.13,0.08);
		_xRatioPart = 4;
		_yRatioPart = 3;
		break;
	}

	_timer.setInterval(1000);
	connect(&_timer,SIGNAL(timeout()),SLOT(onTimer()));
	_timer.start();
}

void ScreenView::onTimer()
{
	if(!_showImage) return;
	if(_screen) delete _screen;
	//_screen = new QPixmap(_width, _height);
	QScreen* screen = QGuiApplication::screens().at(0);
	_screen = new QPixmap(screen->grabWindow(QApplication::desktop()->winId(),screen->geometry().x(),screen->geometry().y()));
	//_screen = new QPixmap(QPixmap::grabWindow(QApplication::desktop()->winId()));
	//_screen = new QPixmap(QPixmap::grabWindow(QApplication::desktop()->winId(),0,0,QApplication::desktop()->width(),QApplication::desktop()->height()));
	//QPainter painter(_screen);
	//painter.fillRect(0,0,_width, _height, QColor(rand()%255,rand()%255,rand()%255));
	//painter.fillRect(10,10,_width-20, _height-20, QColor(rand()%255,rand()%255,rand()%255));
	this->update();
}

void ScreenView::updateView(){
	_ledSize = _height/10;
    _width -= _ledSize*2; _width/=_screenNumber;
	_height -= _ledSize*2;
    double sW = _width, sH = _height; // screen sizes
	if((double)_width/_height > _ratio){
		_width = _height*_ratio;
		_x0 = (sW-_width)/2 + _ledSize;
		_y0 = _ledSize;
	}
	else{
		_height = _width/_ratio;
		_y0 = (sH-_height)/2 + _ledSize;
		_x0 = _ledSize;
	}
	onTimer();
}

QRectF ScreenView::boundingRect() const
{
    return QRectF(_x0, _y0, _width*_screenNumber, _height);
}

QPainterPath ScreenView::shape() const
{
	QPainterPath myPath;
    myPath.addRect(_x0, _y0, _width*_screenNumber, _height);

	return myPath;
}

void ScreenView::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    for(int i=0;i<_screenNumber;i++){
        painter->drawPixmap(_x0+i*_width, _y0, _width, _height, QPixmap(_imageFile));
		if(_showImage)
			painter->drawPixmap(_x0+i*_width+_width*0.063,_y0+_height*0.07,_width*0.874,_height*0.55, *_screen);
		painter->drawPixmap(_x0+i*_width+_width*0.063,_y0+_height*0.07,_width*0.874,_height*0.55, QPixmap(":/images/screenSurface.png"));
    }
}

QList<LedStripe> ScreenView::stripes()
{
	double height = _height*0.71;
    double frW = _width*_frameSize.y();
    double frH = height*_frameSize.x();
	double middle = _x0 + _width/2;
	QList<LedStripe> stripes;
	double w = _xRatioPart;
	double h = _yRatioPart;
	double xPart = (w*_screenNumber)/(w*_screenNumber+h)/2;
	double yPart = h/(w*_screenNumber+h)/2;
    //up
    for(int i=0;i<_screenNumber;i++){
		stripes.append(LedStripe(_x0+frW+i*_width, _y0, _x0+_width-frW+i*_width, _y0, 100.0*xPart/_screenNumber, 90));
    }
    //right
	stripes.append(LedStripe(_x0+_width*_screenNumber, _y0+frH, _x0+_width*_screenNumber, _y0+height-frH, 100.0*yPart, 0));
    //bottom
	for(int i=0;i<_screenNumber;i++){
		stripes.append(LedStripe(_x0+_width-frW+(_screenNumber-1-i)*_width,_y0+height, middle + _width*0.1+(_screenNumber-1-i)*_width, _y0+height, 100.0*xPart/2/_screenNumber, 270));
		stripes.append(LedStripe(middle - _width*0.1+(_screenNumber-1-i)*_width,_y0+height, _x0+frW+(_screenNumber-1-i)*_width, _y0+height, 100.0*xPart/2/_screenNumber, 270));
	}
    //left
	stripes.append(LedStripe(_x0, _y0+height-frH, _x0, _y0+frH, 100.0*yPart, 180));

	return stripes;
}
